package org.jsola.hr.provider.impl;

import lombok.extern.slf4j.Slf4j;
import org.jsola.admin.entity.SiteDO;
import org.jsola.core.Page;
import org.jsola.hr.provider.IAdminProviderService;
import org.jsola.hr.provider.IMessageProviderService;
import org.jsola.hr.vo.SmsConfigSiteNameVO;
import org.jsola.message.dto.EmailDTO;
import org.jsola.message.dto.ShortMessageDTO;
import org.jsola.message.dto.SmsConfigSiteAddDTO;
import org.jsola.message.dto.WeiXinDTO;
import org.jsola.message.entity.SmsConfigSiteDO;
import org.jsola.message.query.SmsConfigSiteQuery;
import org.jsola.message.query.SmsPayLogQuery;
import org.jsola.message.query.SmsSiteQuery;
import org.jsola.message.service.*;
import org.jsola.message.vo.EmailSendLogVO;
import org.jsola.message.vo.SmsPayLogListVO;
import org.jsola.message.vo.SmsSendLogVO;
import org.jsola.message.vo.WeixinTempSendLogVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author zxy
 */
@Slf4j
@Service("hrMessageProviderServiceImpl")
public class MessageProviderServiceImpl implements IMessageProviderService {

    @Autowired
    private ISmsSendService sendShortMessage;

    @Autowired
    private IWeiXinSendService weiXinSendService;

    @Value("${spring.application.name}")
    private String appName;

    @Autowired
    private ISmsSiteService smsSiteService;

    @Autowired
    private ISmsConfigSiteService smsConfigSiteService;

    @Autowired
    private IEmailSendService emailSendService;


    @Override
    public boolean sendShortMessage(ShortMessageDTO shortMessageDTO, TokenUser tokenUser) {
        // 平台
        shortMessageDTO.setAppName(appName);

        SmsSendLogVO smsSendLogVO = sendShortMessage.sendShortMessage(shortMessageDTO, tokenUser);
        return Objects.nonNull(smsSendLogVO);
    }

    @Override
    public boolean verifyCaptcha(String scene, String phone, String captcha) {
        return sendShortMessage.verifyCaptcha(appName, scene, phone, captcha);
    }

    @Override
    public void sendWeChatTempMessage(WeiXinDTO weiXinDTO, TokenUser tokenUser) {
        try {
            weiXinSendService.sendWeiXinTempMessage(weiXinDTO, tokenUser);
        } catch (Exception e) {
            log.info("发送公众号模板消息推送失败,错误信息:{}", e.getMessage());
        }
    }

    @Override
    public List<WeixinTempSendLogVO> batchSendWeChatTempMessage(WeiXinDTO weiXinDTO, TokenUser tokenUser){
        return weiXinSendService.batchSendWeiXinTempMessage(weiXinDTO, tokenUser);
    }

    @Override
    public boolean isSiteMessageEnable(TokenUser tokenUser) {
        return smsSiteService.selectCount(new SmsSiteQuery(), tokenUser.getSiteId()) > 0;
    }

    @Override
    public int getSiteMessageLeftCount(String siteId) {
        int count = -1;
        SmsConfigSiteDO smsConfigSiteDO = smsConfigSiteService.selectBySiteId(siteId);
        //如果启用了配置才返回短信剩余
        if (smsConfigSiteDO != null && smsConfigSiteDO.getEnable()) {
            return smsConfigSiteDO.getQuotaRemain();
        }
        return count;
    }

    @Override
    public EmailSendLogVO sendEmail(EmailDTO emailDTO, TokenUser tokenUser) {
        return emailSendService.sendEmail(emailDTO,tokenUser);
    }

}
